#!/bin/bash

cd frontend

# 设置默认值
HOST="localhost"
PORT="3000"
BACKEND_HOST="localhost"
BACKEND_PORT="8000"

# 解析命令行参数
while [ "$#" -gt 0 ]; do
  case "$1" in
    --host)
      HOST="$2"
      shift 2
      ;;
    --port)
      PORT="$2"
      shift 2
      ;;
    --backend-host)
      BACKEND_HOST="$2"
      shift 2
      ;;
    --backend-port)
      BACKEND_PORT="$2"
      shift 2
      ;;
    *)
      shift
      ;;
  esac
done

echo "启动前端服务器，监听 $HOST:$PORT，后端地址 $BACKEND_HOST:$BACKEND_PORT"

# 设置Vite所需的环境变量
export VITE_HOST="$HOST"
export VITE_PORT="$PORT"
export VITE_BACKEND_HOST="$BACKEND_HOST"
export VITE_BACKEND_PORT="$BACKEND_PORT"

# 使用直接命令行代替npm运行，以确保参数正确传递
npx vite --host "$HOST" --port "$PORT" 